package mxw.suiye.autopydroid.android;


import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;

import mxw.suiye.autopydroid.Server.NetServer;
import mxw.suiye.autopydroid.core.app.App;
import mxw.suiye.autopydroid.core.ui.Ui;
import mxw.suiye.autopydroid.util.MyHandler;

public class MyApp extends Application {

    private NetServer netServer;
    private static Application sApplication;
    private static PackageManager packageManager;

    public static PackageManager getPM() {
        return MyApp.packageManager;
    }

    public static Application getApplication() {
        return sApplication;
    }

    public static Context getContext() {
        return getApplication().getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MyApp.sApplication = this;
        MyApp.packageManager = this.getPackageManager();
        handlerInit();

        Log.d("主程序", "已启动");
        startNetService();
        Ui.alert("AutoPyDroid 服务已启动");
    }

    //启动网络服务
    void startNetService() {
        netServer = new NetServer();
        new Thread(netServer).start();
    }

    void startAccessibility(){
        Intent i = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }

    void handlerInit() {
        MyHandler.setAppHandle(msg -> {
            if (msg.what < 10) {
                switch (msg.what) {
                    case 0:
                        netServer.stop();
                        Process.killProcess(Process.myPid());
                        break;
                    case 1:
                        Ui.toast(msg);
                        break;
                    case 2:
                        Ui.alert(msg);
                        break;
                    case 3:
                        Ui.dialog(msg);
                        break;
                    case 4:
                        Ui.confirm(msg);
                        break;
                    default:
                        break;
                }
            } else if (msg.what < 20) {
                switch (msg.what) {
                    case 11:
                        App.startApp(msg);
                        break;
                    case 12:
                        App.getAppName(msg);
                        break;
                    case 13:
                        break;
                    case 19:
                        startAccessibility();
                        break;
                    default:
                        break;
                }
            }
            return true;
        });
    }
}
